<?php
    
    class Index extends Core{
    	
		protected $weiboUser = null;
    	
		public function __construct(){
			parent::__construct();
		}
		
		public function setPlugIn(){
			return array("saetv2.ex.class.php","page.php");
		}
		
		public function Index(){
			$this->mod = "Index";
			$this->_checkAuth();
			$this->_initUser();
			$this->_getAnnoucement();
			$this->_getDevelopersInfo();
			$this->template();
		}
		
		public function AjaxGetOnline(){
		//	echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
			$uid = $_SESSION['uid'];
			$user_online = DB::fetch_row("select *from `ntwt_user_inline` where `uid`='{$uid}'");
			if($user_online){
				$data = array();
				$data['linktime'] = time();
				$condition = array();
				$condition['uid'] = $uid;
				DB::update("ntwt_user_inline", $data, $condition);
			}else{
				$data = array();
				$data['uid'] = $_SESSION['uid'];
				$data['weibo_name'] = $_SESSION['weibo_name'];
				$data['avatar'] = $_SESSION['avatar'];
				$data['url'] = $_SESSION['url'];
				$data['linktime'] = time();
				DB::insert("ntwt_user_inline", $data);
			}
			$this->AjaxDelteOnline();
			echo json_encode($this->_T);
		}
		
		public function AjaxDelteOnline(){
			$time = time();
			$i = 0;
			$users_inline = DB::fetch_array("select *from `ntwt_user_inline`");
			foreach($users_inline as $key=>$val){
				if(time()-$val['linktime']<20){
				//	$this->_T['users_online'][$i] = $val;
				}else{
					$id=$val['id'];
					DB::Query("delete from `ntwt_user_inline` where `id` ='{$id}' ");
				}
			}
			$this->_T['users_online'] = $users_inline;
			$this->_T['num'] = count($this->_T['users_online']);
//			echo $this->_T['num'];
			
		}
		
		private function _checkAuth(){
			if(!$_SESSION['authorize']){
				$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
                $code_url = $o->getAuthorizeURL( WB_CALLBACK_URL );
				$this->showmessage("检查授权情况，请稍等", $code_url, 2000 ,"我弹我听微博应用");
			}
			return;
		}
		
		private function _initUser(){ //初始化微博用户信息
			$this->weiboUser = new SaeTClientV2(WB_AKEY,WB_SKEY,$_SESSION['token']['access_token']);
			$uid = $this->weiboUser->get_uid();
			$uid = $uid['uid'];
			$this->_T['user']=	$this->weiboUser->show_user_by_id($uid); 
			$_SESSION['uid'] = $this->_T['user']['id'];
			$_SESSION['weibo_name'] = $this->_T['user']['screen_name'];
			$_SESSION['avatar'] = $this->_T['user']['avatar_large'];
			$_SESSION['url'] = "http://weibo.com/u/".$this->_T['user']['id'];
		}
		
		private function _queryUser(){ //在数据库中查询是否有此用户
		    $access_token = $_SESSION['token']['access_token'];
			$uid = $this->weiboUser->get_uid();
			$user  = DB::fetch_row("select * from `ntwt_user` where `uid`='{$uid}'");
			if(!$user){
				$wuser = $this->weiboUser->show_user_by_id($uid);
				$user = array();
				$user['uid']           = $wuser['uid'];
				$user['weibo_name']    = $wuser['screen_name'];
				$user['avatar']        = $wuser['profile_image_url'];
				$user['access_token']  = $access_token;
				$user['last_login_time'] = date('Y-m-d H:i:s');
				DB::insert("ntwt_user", $user);
			}else{
				$data = array();
				$data['access_token']    = $access_token;
				$data['last_login_time'] = date('Y-m-d H:i:s');
				$condition = array();
				$condition['uid'] = $uid;
				DB::update("ntwt_user", $data, $condition);
			}
		}
		
		private function _getAnnoucement(){
			$announcement = DB::fetch_row("select * from `ntwt_site_con` where `id`='1'");
			$this->_T['announcement'] = $announcement['con'];
		}
		
		private function _getDevelopersInfo(){
			$developersInfo = DB::fetch_row("select * from `ntwt_site_con` where `id`='2'");
			$this->_T['developersInfo'] =  $developersInfo['con'];
		}
		
    }











?>